import $ from "jquery";
import "./loading-template.scss";
import { String } from "@/utils/string";

export class Loading {
  private static element: JQuery<HTMLDivElement> | null;
  private static isOpen: boolean = false;

  public static Open(text: string | null = null) {
    if (!Loading.isOpen) {
      if (String.isNullOrWhitespace(text)) {
        Loading.element = $(require("./loading-emtpytext-template.hbs")());
        $("body").prepend(Loading.element);
      } else {
        Loading.element = $(
          require("./loading-template.hbs")({ text })
        );
        $("body").prepend(Loading.element);
      }
      Loading.isOpen = true;
    }
  }

  public static Close() {
    if (Loading.isOpen) {
      Loading.element?.remove();
      Loading.isOpen = false;
      Loading.element = null;
    }
  }
}
